<template>
  <div ref="dom" class="charts chart-line"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'ChartLine',
  props: {
    // value: Object,
    value: Array,
    text: String,
    subtext: String,
    max: Number,
    min: Number
  },
  watch: {
    'value':{//深度监听，可监听到对象、数组的变化
      handler(val, oldVal){
        this.changeChart()
      },
      deep:true
    }
  },
  mounted () {
    this.changeChart()
  },
  methods:{
    changeChart(){
      this.$nextTick(() => {
        let seriesData = this.value.map(_ => _.value)
        let xAxisData = this.value.map(_ => _.time)
        let option = {
          grid: {
            left: '0',
            top: '18px',
            width:'90%',
            height:'80%',
            containLabel:true,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            }
          },
          // toolbox: {
          //   show: true,
          //   feature: {
          //     mark: {show: true},
          //     dataView: {show: true, readOnly: false},
          //     restore: {show: false},
          //     saveAsImage: {show: false}
          //   }
          // },
          xAxis: {
            type: 'category',
            data: xAxisData
          },
          yAxis: {
            show: false,
            type: 'value',
            axisPointer: {
              snap: true
            },
            min: this.min,
            max: this.max
          },
          series: [{
            name: '历史数据值',
            data: seriesData,
            type: 'line',
            areaStyle: {},
          }]
        }
        let dom = echarts.init(this.$refs.dom, 'tdTheme')
        dom.setOption(option)
      })
    }
  }
}
</script>

<style lang="less">

</style>
